package com.canbot.u05.activity.presidemode.newcode;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.customvoice.tool.GridSpacingItemDecoration;
import com.canbot.u05.activity.presidemode.newcode.adapter.NChoiceDanceAdapter;
import com.canbot.u05.activity.presidemode.newcode.bean.DanceBean;
import com.canbot.u05.activity.presidemode.newcode.bean.ModuleUtil;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IHeadCommonClick;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemClick;
import com.canbot.u05.activity.presidemode.newcode.view.HeadCommonView;
import com.canbot.u05.bean.dance.Content;
import com.canbot.u05.bean.dance.DanceListInfo;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.network.DanceUtil;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NChoiceDanceActivity extends BaseHostActivity {
    private int prevSelectPos = 0;
    @BindView(R.id.headCommonView)
    HeadCommonView headCommonView;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    private NChoiceDanceAdapter choiceDanceAdapter;
    private DanceBean selectDance;
    private Dialog dialogLoading;

    @Override
    protected void initEventData() {
        MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(20));
        initIntent();
        initHeadCommon();
        initRecycleView();
    }

    private void initIntent() {
        selectDance = (DanceBean) getIntent().getSerializableExtra(DATA);
    }

    private void initHeadCommon() {
        headCommonView.setiHeadCommonClick(pos -> {
            switch (pos) {
                case IHeadCommonClick.LEFT:
                    finish();
                    break;
                case IHeadCommonClick.RIGHT:
                    if (selectDance != null && selectDance.isSelect()) {
                        Intent intent = new Intent();
                        intent.putExtra(DATA, selectDance);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        Toast.makeText(this, getString(R.string.dance_null_tip), Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        });
    }

    private List<DanceBean> list = new ArrayList<>();

    private void initRecycleView() {
        recycleView.setLayoutManager(new GridLayoutManager(this, 4));
        ((SimpleItemAnimator) recycleView.getItemAnimator()).setSupportsChangeAnimations(false);
        recycleView.addItemDecoration(new GridSpacingItemDecoration(4, (int) getResources().getDimension(R.dimen.x6), false));
        choiceDanceAdapter = new NChoiceDanceAdapter(list, this, new IItemClick() {
            @Override
            public void onClick(int position, View v) {
                if (position != prevSelectPos) {
                    DanceBean danceBean = list.get(prevSelectPos);
                    danceBean.setSelect(false);
                }
                selectDance = list.get(position);
                selectDance.setSelect(!selectDance.isSelect());
                choiceDanceAdapter.notifyItemChanged(prevSelectPos);
                choiceDanceAdapter.notifyItemChanged(position);
                prevSelectPos = position;
                if (selectDance.isSelect()) {
                    ModuleUtil.playDance(selectDance);
                } else {
                    MapVoiceAndEyes.stop();
                }
            }
        });
        recycleView.setAdapter(choiceDanceAdapter);
        getDanceDataFromServer();
    }


    private void getDanceDataFromServer() {
        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, this.getString(R.string.request));
        DanceUtil.INSTANCE.queryAllDance(new DanceUtil.DanceCallBack<DanceListInfo>() {
            @Override
            public void onSuccess(DanceListInfo result) {
                dialogLoading.dismiss();
                Logger.e("DanceFragment", "onSuccess: " + JSON.toJSONString(result));
                List<Content> contentList = result.getContent();
                list.clear();
                for (int i = 0; i < contentList.size(); i++) {
                    Content content = contentList.get(i);
                    DanceBean danceBean = new DanceBean(content.getName(), content.getMusic(), content.getAction(), content.getDefaultImage(), content.getDuration());
                    if (danceBean.equals(selectDance)) {
                        danceBean.setSelect(true);
                        prevSelectPos = i;
                    }
                    list.add(danceBean);
                }
                choiceDanceAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure() {
                dialogLoading.dismiss();
            }
        });
    }


    private List<DanceBean> getDanceData() {
        DanceBean dance1 = new DanceBean("迈克杰克逊", "mj.wav", "10224", R.drawable.mj, 83);
        DanceBean dance2 = new DanceBean("最炫民族风", "zxmzf.wav", "1006", R.drawable.zxmzf, 53);
        DanceBean dance3 = new DanceBean("无法停止的感觉", "music10235.mp3", "10235", R.drawable.wftzdgj, 83);
        DanceBean dance4 = new DanceBean("小苹果", "xpg.mp3", "6", R.drawable.xpg, 58);
        DanceBean dance5 = new DanceBean("龙拳", "dzzl_u05_019_1.wav", "10225", R.drawable.lq, 60);
        DanceBean dance6 = new DanceBean("蓝色多瑙河", "lsdnh.mp3", "10227", R.drawable.lsdnh, 79);
        DanceBean dance7 = new DanceBean("沧海一声笑", "chisx.mp3", "6", R.drawable.chysx, 60);

        DanceBean dance8 = new DanceBean("Despacito", "despacito.mp3", "10237", R.drawable.despacito, 53);
        DanceBean dance9 = new DanceBean("TroubleMaker", "trouble_maker.mp3", "10238", R.drawable.troublemaker, 84);
        DanceBean dance10 = new DanceBean("海草海草", "seaweed_dance.mp3", "10236", R.drawable.haicao, 52);
        DanceBean dance11 = new DanceBean("红昭愿", "hzy.mp3", "10239", R.drawable.hongzhaoyuan, 94);
        ArrayList<DanceBean> danceBeans = new ArrayList<>();
        danceBeans.add(dance1);
        danceBeans.add(dance2);
        danceBeans.add(dance3);
        danceBeans.add(dance4);
        danceBeans.add(dance5);
        danceBeans.add(dance6);
        danceBeans.add(dance7);
        danceBeans.add(dance8);
        danceBeans.add(dance9);
        danceBeans.add(dance10);
        danceBeans.add(dance11);
        if (selectDance != null) {
            for (int i = 0; i < danceBeans.size(); i++) {
                DanceBean danceBean = danceBeans.get(i);
                if (danceBean.equals(selectDance)) {
                    danceBean.setSelect(true);
                    prevSelectPos = i;
                    break;
                }
            }
        }
        return danceBeans;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_nchoice_dance;
    }

    @Override
    protected void onPause() {
        super.onPause();
        MapVoiceAndEyes.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialogLoading != null && dialogLoading.isShowing()) {
            dialogLoading.dismiss();
        }
    }
}
